<template>
  <el-dialog
    :title="dataStructureTitle"
    :visible.sync="dataStructureDialogVisible"
    width="30%"
    :close-on-click-modal="false"
    :before-close="dataStructureDialogClose"
    @close="closeDialog"
  >
      <el-form ref="dataStructureForm" :model="dataStructureForm" :rules="dataStructureRules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="结构名称" prop="structName">
              <el-input v-model="dataStructureForm.structName" placeholder="请输入结构名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="简码">
              <el-input v-model="dataStructureForm.structCode" placeholder="请输入简码"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="是否启用" prop="isOpen">
          <el-radio-group v-model="dataStructureForm.isOpen">
            <el-radio label="1">是</el-radio>
            <el-radio label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            type="textarea"
            v-model="dataStructureForm.remark"
            placeholder="请输入描述信息"
          />
        </el-form-item>
      </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dataStructureConfirm">确定</el-button>
          <el-button @click="dataStructureDialogClose">取 消</el-button>
        </span>
  </el-dialog>
</template>

<script>
import { addStructData, updateStructDetail } from '@/api/system/sort'

export default {
  name: 'dataStructureDialog',
  props:['dataStructureDialogVisible','dataStructureTitle','queryParams','getStruct','dataStructureForm'],
  data() {
    return {
      dataStructureRules:{
        structName: [
          { required: true, message: '请输入结构名称', trigger: 'blur' },
        ],
      }
    };
  },
  methods:{
    closeDialog(){
      this.dataStructureForm.structName = ''
      this.dataStructureForm.structCode = ''
      this.dataStructureForm.remark = ''
    },
    dataStructureDialogClose(){
      this.$emit('closeDStructure')
    },
    dataStructureConfirm(){
      this.$refs.dataStructureForm.validate(async(valid) => {
        if (!valid) {
          return
        }
        this.$confirm('确认提交吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          if(this.dataStructureTitle === '新增数据结构'){
            this.add()
          }else {
            this.update()
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消提交'
          });
        });
      });
    },
    async add(){
      const res = await addStructData(this.dataStructureForm)
      if(res.code === 200){
        this.$message.success(res.msg)
        this.dataStructureDialogClose()
        this.getStruct()
      }
    },
    async update(){
      const res = await updateStructDetail(this.dataStructureForm)
      if(res.code === 200){
        this.$message.success(res.msg)
        this.dataStructureDialogClose()
        this.getStruct()
      }
    }
  },
  watch:{
    queryParams:{
      immediate:true,
      deep:true,
      handler(val){
        this.dataStructureForm.sortNo = val.sortPrantNo
      }
    }
  }
}
</script>

<style scoped>

</style>
